<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CacheFile
 *
 * @author bonfiglio
 */
class ImageFile {

    public $cacheBasePath = false;
    public $sha1 = false;
    public $sha1File = false;
    public $pathfile = false;
    public $thumbFolder = false;
    public $thumb = false;
    private $sizePath = '';
    public $pathinfo = array();
    private $options = array(
        'width' => false,
        'height' => false
    );

    public function __construct($pathfile, $defaultfile, $options = array()) {
        if (!is_file($pathfile)) {
            if (!is_file($defaultfile)) {
                die('WTF !! Le fichier par defaut ' . $defaultfile . " n'est pas un fichier");
            }
            $pathfile = $defaultfile;
        }
        $this->cacheBasePath = CACHE_PATH;
        $this->pathfile = $pathfile;

        $this->pathinfo = pathinfo($pathfile);
        $this->sha1 = sha1_file($pathfile) . '.' . $this->pathinfo['extension'];

        $this->initOptions($options);
        $this->initFileCacheProperties();
        $this->generateThumb();
    }

    private function initFileCacheProperties() {
        $this->sha1File = $this->cacheBasePath . $this->sha1;
        $this->thumbFolder = $this->cacheBasePath . $this->sizePath;
        if (!is_dir($this->thumbFolder)) {
            mkdir($this->thumbFolder);
        }
        $this->thumb = $this->thumbFolder . $this->sha1;
    }

    private function initOptions($options) {
        $this->options = array_merge($this->options, $options);
        $this->options['width'] = (int) $this->options['width'];
        $this->options['height'] = (int) $this->options['height'];

        //Déterminer la taille
        $size = '';
        if ($this->options['width'] > 0) {
            $size .= 'w' . $this->options['width'];
        }
        if ($this->options['height'] > 0) {
            $size .= 'h' . $this->options['height'];
        }
        if (strlen($size)) {
            $size .= '/';
        }
        $this->sizePath = $size;
    }

    private function generateThumb() {
        $res = $tmp_img = null;
        @unlink($this->thumb);

        if (!is_file($this->thumb)) {
            // Lit les dimensions de l'image source
            list($width, $height) = getimagesize($this->pathfile);

            // calculate thumbnail size
            if ($width > $this->options['width'] && $this->options['width'] != 0) {
                switch ($this->pathinfo['extension']) {
                    case 'png':
                        $res = imagecreatefrompng($this->pathfile);
                        break;
                    case 'gif':
                        $res = imagecreatefromgif($this->pathfile);
                        break;
                    case 'jpg':
                    case 'jpeg':
                        $res = imagecreatefromjpeg($this->pathfile);
                        break;
                }
                $new_width = $this->options['width'];
                $new_height = floor($height * ( $this->options['width'] / $width ));

                // create a new temporary image
                $tmp_img = imagecreatetruecolor($new_width, $new_height);

                // preserve transparency
                if (in_array($this->pathinfo['extension'], array('gif', 'png'))) {
                    imagecolortransparent($tmp_img, imagecolorallocatealpha($tmp_img, 0, 0, 0, 127));
                    imagealphablending($tmp_img, false);
                    imagesavealpha($tmp_img, true);
                }

                // copy and resize old image into new image 
                imagecopyresized($tmp_img, $res, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

                switch ($this->pathinfo['extension']) {
                    case 'png':
                        imagepng($tmp_img, $this->thumb);
                        break;
                    case 'gif':
                        imagegif($tmp_img, $this->thumb);
                        break;
                    case 'jpg':
                    case 'jpeg':
                        imagejpeg($tmp_img, $this->thumb);
                        break;
                }
            } else {
                switch ($this->pathinfo['extension']) {
                    case 'png':
                        $res = imagecreatefrompng($this->pathfile);
                        imagepng($res, $this->thumb);
                        break;
                    case 'gif':
                        $res = imagecreatefromgif($this->pathfile);
                        imagegif($res, $this->thumb);
                        break;
                    case 'jpg':
                    case 'jpeg':
                        $res = imagecreatefromjpeg($this->pathfile);
                        imagejpeg($res, $this->thumb);
                        break;
                }
            }
        }
    }

    public function display() {
        ini_set("zlib.output_compression", "Off");
        if (file_exists($this->thumb)) {
            $fp = fopen($this->thumb, 'rb');
            header('Content-type: image/' . $this->pathinfo['extension']);
            header("Content-Length: " . filesize($this->thumb));
            fpassthru($fp);
        } else {
            Log::pr($this->thumb);
        }
        exit;
    }

}

?>
